package com.music.music.service.impl;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

import com.music.common.core.constant.Constants;
import com.music.common.redis.service.RedisService;
import com.music.music.domain.MusicType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.music.music.mapper.MusicTypeMapper;
import com.music.music.domain.MusicType;
import com.music.music.service.MusicTypeService;

import javax.annotation.Resource;

/**
 * 音乐类型Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-22
 */
@Service
public class MusicTypeServiceImpl implements MusicTypeService {
    private final ReentrantLock lock = new ReentrantLock();
    @Resource
    private MusicTypeMapper musicTypeMapper;
    @Resource
    RedisService redisService;

    /**
     * 查询音乐类型
     *
     * @param musicTypeId 音乐类型主键
     * @return 音乐类型
     */
    @Override
    public MusicType selectMusicTypeByMusicTypeId(Integer musicTypeId) {
        return musicTypeMapper.selectMusicTypeByMusicTypeId(musicTypeId);
    }

    /**
     * 查询音乐类型列表
     *
     * @param musicType 音乐类型
     * @return 音乐类型
     */
    @Override
    public List<MusicType> selectMusicTypeList(MusicType musicType) {
        return musicTypeMapper.selectMusicTypeList(musicType);
    }

    @Override
    public List<MusicType> selectMusicTypeListWeb(MusicType musicType) {

        // 取redis
        List<MusicType> musicTypeList = redisService.getCacheList(Constants.MUSIC_TYPE_LIST_KEY);

        if (musicTypeList == null || musicTypeList.isEmpty()) {
            lock.lock();
            try {
                // 取redis
                musicTypeList = redisService.getCacheList(Constants.MUSIC_TYPE_LIST_KEY);
                if (musicTypeList == null || musicTypeList.isEmpty()) {
                    musicTypeList = musicTypeMapper.selectMusicTypeList(musicType);

                    // 存redis
                    redisService.setCacheList(Constants.MUSIC_TYPE_LIST_KEY, musicTypeList);

                    // 设置随机过期时间
                    int randomExpiration = ThreadLocalRandom.current().nextInt(5, 16);
                    redisService.expire(Constants.MUSIC_TYPE_LIST_KEY, randomExpiration, TimeUnit.MINUTES);
                }
            } finally {
                lock.unlock();
            }
        }

        return musicTypeList;
    }

    /**
     * 新增音乐类型
     *
     * @param musicType 音乐类型
     * @return 结果
     */
    @Override
    public int insertMusicType(MusicType musicType) {
        return musicTypeMapper.insertMusicType(musicType);
    }

    /**
     * 修改音乐类型
     *
     * @param musicType 音乐类型
     * @return 结果
     */
    @Override
    public int updateMusicType(MusicType musicType) {
        return musicTypeMapper.updateMusicType(musicType);
    }

    /**
     * 批量删除音乐类型
     *
     * @param musicTypeIds 需要删除的音乐类型主键
     * @return 结果
     */
    @Override
    public int deleteMusicTypeByMusicTypeIds(Integer[] musicTypeIds) {
        return musicTypeMapper.deleteMusicTypeByMusicTypeIds(musicTypeIds);
    }

    /**
     * 删除音乐类型信息
     *
     * @param musicTypeId 音乐类型主键
     * @return 结果
     */
    @Override
    public int deleteMusicTypeByMusicTypeId(Integer musicTypeId) {
        return musicTypeMapper.deleteMusicTypeByMusicTypeId(musicTypeId);
    }
}
